 package com.soosync.common;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.json.JSONObject;

public abstract class AbstractHttpServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static JSONObject requestBox = null;
	protected static JSONObject resultBox = new JSONObject();
	protected static HttpSession session = null;

	public AbstractHttpServlet() {
		super();
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		start(request,response);
		process();
		end(request,response);
	}

	private void start(HttpServletRequest request, HttpServletResponse response){
		requestBox = CollectionUtility.getJSONBox(request);
		session = request.getSession(true);
	}
	
	protected abstract void process();
	
	private void end(HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setContentType("application/json; charset=utf-8");
		PrintWriter printWriter = response.getWriter();
		printWriter.print(resultBox.toString());
		printWriter.flush();
		printWriter.close();
	}
}